package com.example.filesystem.controller;

import com.example.filesystem.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.example.filesystem.interceptor.LimitFlowInterceptor.flowInterUuidPrefix;

/**
 * @author bbyh
 * @since 2025-04-30
 */
@RestController
@RequestMapping("/download")
@Slf4j
@Api("/下载口令接口")
public class DownloadController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("下载口令生成接口")
    @GetMapping("/generateDownloadToken")
    public Result generateDownloadToken(HttpServletRequest request) {
        try {
            String username = request.getHeader("username");
            String uuid = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set(flowInterUuidPrefix + uuid, username, 60, TimeUnit.SECONDS);
            return Result.success(uuid, "uuid生成成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(null, "下载口令生成接口服务出现错误");
        }
    }
}
